package com.suse.lhw.resourceservere.controller;

import com.suse.lhw.hms.exersiseserver.domin.EUnit;
import com.suse.lhw.hms.exersiseserver.pojo.out.MovementModelAndUnits;
import com.suse.lhw.resourceservere.POJO.output_POJO.eMoventmodel_units;
import com.suse.lhw.resourceservere.client.exersicserverClient;
import com.suse.lhw.resourceservere.constants.ConstStatuMovementModel;
import com.suse.lhw.resourceservere.utils.FileUploader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ProjectName: health_manage_system
 * @Package: com.suse.lhw.resourceservere.com.suse.lhw.security.controller
 * @ClassName: MovementController
 * @Description: java类作用描述
 * @Author: 李鸿炜
 * @CreateDate: 2020/6/4 3:12
 * @UpdateUser: 更新者
 * @UpdateDate: 2020/6/4 3:12
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
@RestController
@RequestMapping("movuser")
public class MovementController {
    @Autowired
    private exersicserverClient exersicserverClient;

    @RequestMapping("excuteAMdoel")
    public Map<String, Object> ExcuteAModel(@RequestBody String json) {
        /**m
         * @Author:Hongwei Li
         * @Description:增加一个运动模型实例
         * @Param:[]
         * @Return:java.util.Map<java.lang.String,java.lang.Object>
         **/
//        System.out.println("mm is"+mMandEUnit+"quanlity is"+mMandEUnit.getQuality());
        Map<String, Object> map = new HashMap<String, Object>();

        String result = exersicserverClient.excuteAMdoel(json);
        if (result == null)
            map.put("returnMsg", "出现了问题");
        else
            map.put("returnMsg", result);
        return map;

    }

    @ResponseBody
    @RequestMapping("addADIYmodel")
    public Map<String, Object> addADIYmodel(@RequestPart MultipartFile file, @RequestParam String mmName, @RequestParam String mmImgurl,
                                            @RequestParam int mmCategoryId, @RequestParam int status, @RequestParam int mmPackagetype,
                                            @RequestParam String mmDescription, @RequestParam String difficulty,
                                            @RequestParam String points, @RequestParam String unitNameString, @RequestParam String unitConsumString) {
        /**
         * @Author:Hongwei Li
         * @Description:增加一个自定义的模型
         * @Date:3:56 2020/6/4
         * @Param:[eMoventmodel_units]
         * @Return:java.util.Map<java.lang.String,java.lang.Object>
         **/
        Map<String, Object> map = new HashMap<String, Object>();
        //设置状态为未上传
        eMoventmodel_units eMoventmodel_units = new eMoventmodel_units();
        eMoventmodel_units.setMmName(mmName);
        eMoventmodel_units.setMmImgurl(mmImgurl);
        eMoventmodel_units.setMmCategoryId(mmCategoryId);
        eMoventmodel_units.setDifficulty(difficulty);
        eMoventmodel_units.setMmPackagetype(mmPackagetype);
        eMoventmodel_units.setMmDescription(mmDescription);
        eMoventmodel_units.setPoints(points);
        eMoventmodel_units.setStatus(ConstStatuMovementModel.un_upload);
        eMoventmodel_units.setUserid("aaa");
        List<EUnit> units = new ArrayList<EUnit>();
        //将传过来的单位拼接一遍
        String[] unitNames = unitNameString.split("@");
        String[] unitConsums = unitConsumString.split("@");
        for (int i = 0; i < unitNames.length; i++) {
            units.add(new EUnit(unitNames[i], Integer.parseInt(unitConsums[i])));
            System.out.println(units.toString());
        }
        eMoventmodel_units.setUnits(units);
        System.out.println("the emu is" + eMoventmodel_units);
        //上传文件
        String fileloadResult = FileUploader.uploadAImg(file);
        System.out.println(fileloadResult);
        eMoventmodel_units.setMmImgurl(fileloadResult);
        String result = exersicserverClient.addADIYModel(eMoventmodel_units);
        if (result == null) {
            map.put("returnMsg", "添加失败！");
        } else
            map.put("returnMsg", result);
        return map;
    }


    @ResponseBody
    @RequestMapping("selectMMByNameUseridAndStatus")
    public Map<String, Object> selectMMByNameUseridAndStatus(@RequestParam int status, @RequestParam(required = false) String name) {
        System.out.println("status is" +
                status +
                "name is" +
                name);
        Map<String, Object> map = new HashMap<String, Object>();
        String userId = "aaa";
        List<MovementModelAndUnits> list = exersicserverClient.selectByNameUseridAndStatus(name, status, userId);
        if (list == null) {
            map.put("returnMsg", "没有找到哦！");
        } else {
            for (MovementModelAndUnits movementModelAndUnits : list) {
                System.out.println(movementModelAndUnits.toString());
            }
            map.put("returnMsg", "找到了" + list.size() + "个运动");
            for (MovementModelAndUnits movementModelAndUnits : list) {
                System.out.println(movementModelAndUnits.toString());
            }
        }
        map.put("list", list);
        return map;
    }


    @ResponseBody
    @RequestMapping("deleteMovModel")
    public Map<String, Object> deleteMovModel(@RequestParam Integer monId) {
        Map<String, Object> map = new HashMap<>();
        //先得到文件名再删除运动模型
        com.suse.lhw.resourceservere.POJO.output_POJO.MovementModelAndUnits eMoventmodel = exersicserverClient.selectModelById(monId);
        int resutl = exersicserverClient.deleteAMovModel(monId);
        //再删文件
        File file = new File("D:\\AppFilesWithNoPriority\\毕业设计\\项目\\减肥管理系统\\health_manage_system\\resourceservere\\src\\main\\resources\\static\\image\\userImg\\" + eMoventmodel.getMmImgurl());
        file.delete();
        if (resutl == 1) {
            map.put("sign", 1);
            map.put("msg", "朋友，事情帮你办好了！");
        } else {
            map.put("sign", 0);
            map.put("msg", "对不起，是我办事不利");
        }
        return map;
    }


    @ResponseBody
    @RequestMapping("deleteMovInstance")
    public Map<String, Object> deleteMov(@RequestParam Integer movInstanceId) {
        System.out.println("this mobInstanceId is" + movInstanceId);
        Map<String, Object> map = new HashMap<String, Object>();
        int resutl = exersicserverClient.deleteMovInstance(movInstanceId);
        if (resutl == 0)
            map.put("returnMsg", "删除失败");
        else
            map.put("returnMsg", "删除成功");
        return map;
    }


}
